package com.ass.zookeeper.service.impl;

import com.ass.zookeeper.constant.ServerNodeType;
import com.ass.zookeeper.service.ZkApiService;
import lombok.RequiredArgsConstructor;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ZkApiServiceImpl implements ZkApiService {

    private final CuratorFramework curatorClient;


    @Override
    public void create(String path, ServerNodeType serverNodeType, String data) {
        List<ACL> aclList = new ArrayList<>();
        //可以是多个
        Id id = new Id("world", "anyone");
        aclList.add(new ACL(ZooDefs.Perms.ALL, id));
        try {
            curatorClient.create()
                    .creatingParentsIfNeeded()  //没有父节点时 创建父节点
                    .withMode(serverNodeType.createMode.PERSISTENT)  //节点类型
                    .withACL(aclList)   //配置权限
                    .forPath(path, data.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private List<ACL> setAcl(String acl){
        //

        return null;
    }



}
